package com.aube.mis.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aube.mis.dto.ActorDTO;
import com.aube.mis.dto.AdvertisingDTO;
import com.aube.mis.dto.AppModelConfigDTO;
import com.aube.mis.dto.ItemDTO;
import com.aube.mis.dto.LotteryDTO;
import com.aube.mis.dto.ModelDTO;
import com.aube.mis.dto.PicDTO;
import com.aube.mis.dto.RelatedCallbackDTO;
import com.aube.mis.dto.ShowDTO;
import com.aube.mis.dto.ShowModelDTO;
import com.aube.mis.dto.TemplateConfigDTO;
import com.aube.mis.dto.TemplateDTO;
import com.aube.mis.dto.VideoDTO;
import com.aube.mis.dto.VideoThemeDTO;
import com.aube.mis.enums.ResourceType;
import com.aube.mis.service.LotteryService;
import com.aube.mis.service.ResourceService;
import com.aube.mis.service.ShowService;
import com.aube.mis.service.ShowSettingService;
import com.aube.mis.service.ThemeService;
import com.aube.mis.util.JsonUtil;
import com.aube.mis.vo.DataTableCriterias;

@Controller
@RequestMapping(value = "/show/setting")
public class ModelController extends AbstractController {

	private static Logger log = LoggerFactory.getLogger(ModelController.class);

	@Resource
	ShowService showService;

	@Resource
	ShowSettingService showSettingService;

	@Resource
	ResourceService resourceService;

	@Resource
	ThemeService themeService;

	@Resource
	LotteryService lotteryService;

	@RequestMapping(value = "")
	public String setting(HttpServletResponse response, ModelMap map, @ModelAttribute RelatedCallbackDTO relatedCallbackDTO) {
		if ("1".equals(relatedCallbackDTO.getIsRelatedCallback())) {
			if ("theme".equals(relatedCallbackDTO.getRelatedType())) {
				themeService.updateDefaultShowTheme(relatedCallbackDTO.getSourceId(), relatedCallbackDTO.getSelectedRelatedIds());
			}
		}
		ShowDTO showDetail = showService.getShowDetail(getShowId());
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		VideoThemeDTO defaultTheme = themeService.getShowDefaultTheme(getShowId());
		map.put("show", showDetail);
		map.put("defaultTheme", defaultTheme);
		return "/show/setting/base";
	}

	@RequestMapping(value = "/model/sort.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String sort(String modelIds) {
		List<String> modelIdList = Arrays.asList(modelIds.split(","));
		showSettingService.sortModelList(getShowId(), modelIdList);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/model/add", method = RequestMethod.GET)
	public String modelAdd(HttpServletResponse response, ModelMap map, String templateCode, String clientType) {
		List<AppModelConfigDTO> appModelConfigDTO = showSettingService.getAppModelConfigList(getShowId(), templateCode, clientType);
		map.put("clientType", clientType);
		map.put("templateCode", templateCode);
		map.put("appModelConfigList", appModelConfigDTO);
		map.put("appModelConfigListJson", JsonUtil.toJson(appModelConfigDTO));
		return "/show/setting/model/add.page";
	}

	@RequestMapping(value = "/model/edit")
	public String modelEdit(HttpServletResponse response, ModelMap map, String modelId, @ModelAttribute RelatedCallbackDTO relatedCallbackDTO) {
		ModelDTO modelDetail = showSettingService.getModelDetail(getShowId(), modelId);
		if ("1".equals(relatedCallbackDTO.getIsRelatedCallback())) {
			modelDetail.setRelatedIds(relatedCallbackDTO.getSelectedRelatedIds());
			modelDetail = showSettingService.editModel(modelDetail);
		}
		if (StringUtils.isNotEmpty(modelDetail.getRelatedIds())) {
			List<?> relatedList = getRelatedData(modelDetail.getRelatedType(), modelDetail.getRelatedIds());
			map.put("relatedList", relatedList);
			map.put("relatedListJson", JsonUtil.toJson(relatedList));
		}
		map.put("model", modelDetail);
		map.put("modelId", modelDetail.getModelId());
		List<AppModelConfigDTO> appModelConfigDTO = showSettingService.getAppModelConfigList(getShowId(), modelDetail.getTemplateCode(), modelDetail.getClientType());
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		map.put("appModelConfigList", appModelConfigDTO);
		map.put("appModelConfigListJson", JsonUtil.toJson(appModelConfigDTO));
		map.put("templateCode", modelDetail.getTemplateCode());
		return "/show/setting/model/edit";
	}

	private List<?> getRelatedData(String relatedType, String relatedIds) {
		if (StringUtils.isEmpty(relatedType)) {
			return null;
		}
		if (relatedType.equals("episode")) {
			List<VideoDTO> videoList = resourceService.getVideoList(Arrays.asList(relatedIds.split(",")));
			return videoList;
		} else if (relatedType.equals("stagePhoto")) {
			List<PicDTO> picList = resourceService.getPicList(Arrays.asList(relatedIds.split(",")));
			return picList;
		} else if (relatedType.equals("actor")) {
			List<ActorDTO> actorList = resourceService.getActorList(Arrays.asList(relatedIds.split(",")));
			return actorList;
		} else if (relatedType.equals("goods")) {
			List<ItemDTO> itemList = resourceService.getItemList(Arrays.asList(relatedIds.split(",")));
			return itemList;
		} else if (relatedType.equals("drawActivity")) {
			LotteryDTO lotteryDetail = lotteryService.getLotteryDetail(relatedIds);
			return Arrays.asList(lotteryDetail);
		}
		return null;
	}

	@RequestMapping(value = "/model/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String modelEditAjax(@ModelAttribute ModelDTO modelDTO) {
		ModelDTO result = showSettingService.editModel(modelDTO);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("result", result);
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/model/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String modelDelete(String modelId) {
		showSettingService.deleteModel(getShowId(), modelId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/model/sortResource.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String sortResource(String modelId, String templateCode, String resourceType, String resourceIds) {
		if(resourceType.equals("episode")) {
			resourceType = ResourceType.VIDEO.getCode();
		}
		resourceService.relateResource(getShowId(), templateCode, modelId, resourceType, Arrays.asList(resourceIds.split(",")));
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/model/saveExtraDesc.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String saveExtraDesc(String modelId, String extraDescJson) {
		showSettingService.saveExtraDesc(getShowId(), modelId, extraDescJson);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/advertising/list")
	public String advertisingList(HttpServletResponse response, ModelMap map, String clientType, String templateCode, @ModelAttribute RelatedCallbackDTO relatedCallbackDTO) {
		if(StringUtils.isEmpty(clientType)) {
			clientType = "mobile";
		}
		if ("1".equals(relatedCallbackDTO.getIsRelatedCallback()) && StringUtils.isNotEmpty(relatedCallbackDTO.getSelectedRelatedIds())) {
			if("advertising-relate-video".equals(relatedCallbackDTO.getSourceId())) {
				showSettingService.sortAdvertisingVideo(getShowId(), clientType, templateCode, relatedCallbackDTO.getSelectedRelatedIds());
			} else {
				showSettingService.relateAdvertising(relatedCallbackDTO.getSourceId(), relatedCallbackDTO.getSelectedRelatedIds());
			}
		}
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		List<String> videoIdList = showSettingService.getAdvertisingRelatedVideo(getShowId(), clientType, templateCode);
		if(CollectionUtils.isNotEmpty(videoIdList)) {
			List<VideoDTO> videoList = resourceService.getVideoList(videoIdList);
			map.put("videoListJson", JsonUtil.toJson(videoList));
		}
		map.put("clientType", clientType);
		map.put("templateCode", templateCode);
		return "/show/setting/advertising/list";
	}

	@RequestMapping(value = "/advertising/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String advertisingData(@ModelAttribute DataTableCriterias criterias, String clientType, String templateCode) {
		criterias.setLength(9999);
		List<AdvertisingDTO> list = showSettingService.getAdvertisingList(criterias, getShowId(), clientType, templateCode);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", list);
		dataMap.put("total", list.size());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/advertising/edit", method = RequestMethod.GET)
	public String advertisingEdit(HttpServletResponse response, ModelMap map, String advertisingId, String clientType, String templateCode) {
		if (StringUtils.isEmpty(advertisingId)) {
			map.put("isEdit", false);
			map.put("clientType", clientType);
			map.put("templateCode", templateCode);
		} else {
			map.put("isEdit", true);
			AdvertisingDTO advertisingDetail = showSettingService.getAdvertisingDetail(getShowId(), advertisingId);
			map.put("advertising", advertisingDetail);
			map.put("clientType", advertisingDetail.getClientType());
			map.put("templateCode", advertisingDetail.getTemplateCode());
		}
		return "/show/setting/advertising/edit.page";
	}

	@RequestMapping(value = "/advertising/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String advertisingEditAjax(@ModelAttribute AdvertisingDTO advertisingDTO) {
		advertisingDTO.setShowId(getShowId());
		showSettingService.editAdvertising(advertisingDTO);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/advertising/sortVideo.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String advertisingSortVideoAjax(String clientType, String templateCode, String videoIds) {
		showSettingService.sortAdvertisingVideo(getShowId(), clientType, templateCode, videoIds);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/advertising/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String advertisingDelete(String advertisingId) {
		showSettingService.deleteAdvertising(getShowId(), advertisingId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/model/select")
	public String modelSelect(HttpServletResponse response, ModelMap map, String selectedModelIds, String resourceType, String callbackUrl, String relatedIds, String sourceId) {
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		map.put("selectedModelIds", selectedModelIds);
		map.put("callbackUrl", callbackUrl);
		map.put("relatedIds", relatedIds);
		map.put("resourceType", resourceType);
		map.put("sourceId", sourceId);
		return "/show/setting/model/select";
	}

	@RequestMapping(value = "/model/getRelatedModelIds.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String getRelatedModelIds(String resourceId, String resourceType) {
		List<String> relatedModelIdList = showSettingService.getModelIdsByResource(getShowId(), resourceType, resourceId);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("relatedModelIds", relatedModelIdList.toString().replaceAll("[\\[.\\].\\s+]", ""));
		return this.createSuccessResponse(dataMap);
	}
}
